# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2JsonByYM
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField_YM
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

import time
localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

class PointItemsDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def add_point_items(self, col_info):

        point_item_name = col_info['point_item_name']
        is_per_day = col_info['is_per_day']
        point_item_point = col_info['point_item_point']
        point_item_remark = col_info['point_item_remark']
        point_item_sort = col_info['point_item_sort']

        sql = "insert into zzh_point_items (point_item_name,is_per_day,point_item_point,point_item_remark,point_item_sort) VALUES (%s,%s,%s,%s,%s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [point_item_name, is_per_day, point_item_point, point_item_remark, point_item_sort])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def del_point_items(self, ids):
        cur = self.conn.cursor()
        sql = "delete from zzh_point_items WHERE point_item_id=%s"
        for id in ids:
            cur.execute(sql, [id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return 1

    def query_point_items(self, point_item_id):

        sql = "select * from zzh_point_items WHERE point_item_id=%s "
        cur = self.conn.cursor()
        total_count  = cur.execute(sql, [point_item_id])
        if total_count:
            point_item_list = cur.fetchall()
            cur.close()
            self.conn.close()
            point_item_json = Table2JsonByYM.point_item_listto_json( point_item_list)
            return total_count,point_item_json
        else :
            return 0,None

    def queryPointItemsByCondition(self,**kwargs):
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']




        fieldMap = requestFieldMapToDBField_YM.pointItemsMap#要改的这个
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM zzh_point_items WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM zzh_point_items WHERE " + whereSentence
        rs = cur.execute(sql)

        total_count = cur.execute(sql)
        if total_count:
            result = cur.fetchall()

            cur.close()
            self.conn.close()
            resultList= MyPaging().getPage(total_count,pageIndex,pageSize,result)
            userOauthJSON =Table2JsonByYM.point_item_listto_json(resultList)
            return total_count,userOauthJSON
        else:

            cur.close()
            self.conn.close()
            return 0,None




    def selectPointItems(self, id):
        """
        根据ID查询信息，返回元组

        """
        sql = "select * from zzh_point_items WHERE point_item_id=%s "

        cur = self.conn.cursor()
        rs = cur.execute(sql, [id])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr



    def update_point_items(self, new_point_item, point_item_id):
        point_item_name = new_point_item['point_item_name']
        is_per_day = new_point_item['is_per_day']
        point_item_point = new_point_item['point_item_point']
        point_item_remark = new_point_item['point_item_remark']
        point_item_sort = new_point_item['point_item_sort']
        cur = self.conn.cursor()
        sql = "UPDATE zzh_point_items SET point_item_name=%s,is_per_day=%s," \
              "point_item_point=%s,point_item_remark=%s ,point_item_sort=%s WHERE point_item_id=%s "
        update_point_item = cur.execute(sql,[point_item_name, is_per_day, point_item_point, point_item_remark, point_item_sort, point_item_id])
        self.conn.commit()
        if update_point_item:
            cur.close()
            self.conn.close()
            return 1
        else:
            cur.close()
            self.conn.close()
            return 0
